﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class RoleDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// List all roles 
        /// </summary>
        /// <returns>List of role</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/3</date>
        public List<Role> LoadRoles()
        {
            try
            {
                List<Role> listRole = new List<Role>();
                var roles = from role in _entities.Roles 
                            where role.IsDelete == false 
                            select role;
                if (roles != null)
                {
                    listRole = roles.ToList();
                }
                return listRole;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Load role by Id 
        /// </summary> 
        /// <param name="roleId"></param>
        /// <returns>A current role</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/3</date>
        public Role SelectRole(int roleId)
        {
            try
            {
                Role role = new Role();
                role = (from roleEnt in _entities.Roles
                        where roleEnt.RoleId == roleId
                            && roleEnt.IsDelete == false
                        select roleEnt).FirstOrDefault();
                return role;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add a Role
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Add(Role role)
        {
            try
            {
                _entities.Roles.Add(role);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if role is existed or not
        /// </summary>
        /// <param name="rol"></param>
        /// <returns>true || false</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public bool CheckIsExisted(Role rol)
        {
            try
            {
                var isExist = _entities.Roles.Any(r => r.RoleName == rol.RoleName);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit a Level
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Edit(Role role)
        {
            try
            {
                _entities.Entry(role).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete a Level
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Delete(Role role)
        {
            try
            {
                role.IsDelete = true;
                _entities.Entry(role).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get roleId by Role Name
        /// </summary>
        /// <param name="name"></param>
        /// <returns>Role Id</returns>
        /// <author>hungph</author>
        /// <date>3/8/2014</date>
        public int GetRoleIdByName(String name)
        {
            try
            {
                int roleId = 0;
                var roles = from role in _entities.Roles where role.RoleName == name select role;
                foreach (var role in roles)
                {
                    roleId = role.RoleId;
                }
                return roleId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}